package main

import (
	//"io/ioutil"
	"net/http"
	"fmt"
	"encoding/json"
)

type Cooks struct {
	Status bool
	Tngou  []Cook
}

//做法
type Cook struct {
	Id          int
	Name        string //名称
	Food        string //食物
	Img         string //图片
	Images      string //图片,
	Description string //描述
	Keywords    string //关键字
	Message     string //资讯内容
	Count       int    //访问次数
	Fcount      int    //收藏数
	Rcount      int    //评论读数
			   // Url         string
}

func Fetch(name string) ([]Cook, error) {

	//name := "小炒肉"
	resp, err := http.Get("http://www.tngou.net/api/cook/name?name=" + name)
	//resp, err := http.Get("http://www.tngou.net/api/cook/show?id=98302")
	if err != nil {
		// handle error
	}
	defer resp.Body.Close()

	//body, err := ioutil.ReadAll(resp.Body)
	//if err != nil {
	//	// handle error
	//}
	//
	//fmt.Println(string(body))

	decoder := json.NewDecoder(resp.Body)

	//cook :=Cook{}
	//var cook map[string]interface{}
	//var cook Cooks
	cook := new(Cooks) // or &Cooks{}
	//cook := new(Cook)
	err = decoder.Decode(cook)
	if err != nil {
		// handle error
		fmt.Println(err)
	}
	//fmt.Printf("a %s", "string")
	//fmt.Printf("cook: %+v", cook)
	fmt.Println(len(cook.Tngou))

	return cook.Tngou, nil
}

//func httpGet() {
//
//	resp, err := http.Get("http://www.01happy.com/demo/accept.php?id=1")
//	if err != nil {
//		// handle error
//	}
//
//	defer resp.Body.Close()
//	body, err := ioutil.ReadAll(resp.Body)
//	if err != nil {
//		// handle error
//	}
//
//	fmt.Println(string(body))
//}
